package cn.tedu.basic;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

//测试读取流
public class Test2_In {
    public static void main(String[] args) throws IOException {
        Method();
        Method2();
    }

    //读取普通流,FileInputStream,以字节为单位读取，比较慢
    public static void Method() throws IOException {
        //创建子类对象，只提供含参构造
        //FileInputStream(File file);要创建File对象
        //FileInputStream(String name);name是文件路径
        //File f =new File("D:\\zhang");
        //InputStream I =new FileInputStream(f);
        //将文件路径放进文件流
        InputStream Ist = new BufferedInputStream(new FileInputStream("D:\\a\\b\\link.jpg"));
        Ist.read();
        Ist.close();
        InputStream In = new FileInputStream("D:\\a\\b\\2.txt");//利用多态
        //开始读取
        int data = In.read();
        System.out.println(data);
        int data1 = In.read();
        System.out.println(data1);
        int data2 = In.read();
        System.out.println(data2);
        int data3 = In.read();
        System.out.println(data3);
        //释放资源
        In.close();
        //更改代码
//        int a =In.read();
//        while(a!=-1){
//            System.out.println(a);
//        }
//        In.close();//死循环
        int b = 0;
        while ((b = In.read()) != -1) {
            System.out.println(b);
        }
        In.close();
    }

    //创建多态对象测试BufferedInputStream缓冲流/高级流，以数组为单位读取，提高效率，底层维护一个缓冲数组
    //byte[] buf 默认容量8192的容量，相当于8k的容量,一次性将数组里的数据批量读到程序里，
    public static void Method2() throws IOException {
        InputStream Is = new BufferedInputStream(new FileInputStream("D\\a\\b\\2.txt"));
        int a = Is.read();
        System.out.println(a);
        int aa = 0;
        while ((aa = Is.read()) != -1) {
            System.out.println(aa);
        }
        Is.close();
        InputStream Ist = new BufferedInputStream(new FileInputStream("D:\\a\\b\\link.jpg"));
        Ist.read();
        Ist.close();
    }
}

